#region (c) 2014 THN Solutions LLC. - All rights reserved
/*
Simplified BSD License (BSD)
Copyright (c) 2014, THN Solutions LLC. ( www.ThnSolutions.com )
Author: Nguyễn, M. Hải                 ( www.Minh-Hai.com     )
All rights reserved.

Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:

    * Redistributions of source code must retain the above copyright notice, this 
      list of conditions and the following disclaimer.

    * Redistributions in binary form must reproduce the above copyright notice, 
      this list of conditions and the following disclaimer in the documentation 
      and/or other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 
IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
POSSIBILITY OF SUCH DAMAGE.
*/
#endregion

#region Record of Change
//             							CHANGE HISTORY
// -------------------------------------------------------------------------------------
// |   DATE    | DEVELOPER  | DESCRIPTION                                              |
// -------------------------------------------------------------------------------------
// | 09-Nov-09 | HaiNM      | First creation.                                          |
// -------------------------------------------------------------------------------------
//
#endregion

#region Record of Audit
//             							REVIEW HISTORY
// -------------------------------------------------------------------------------------
// |   DATE    | REVIEWER   | NOTES                                                    |
// -------------------------------------------------------------------------------------
// -------------------------------------------------------------------------------------
//
#endregion

#region Using directives
using System;
using System.Drawing;
using System.ComponentModel;

using Thn;
#endregion

namespace Thn.Interface.Vcl
{
    /// <summary>
    /// Display a progressing bar based on percentage of value.
    /// </summary>
    [ToolboxItem(true), DefaultProperty("Value"), DefaultEvent("ValueChanged"), ToolboxBitmap(typeof(ResourceBeacon), "Thn.Interface.Vcl.Resources.Icons.ProgressBar.bmp")]
#if DEBUG
    public
#else
    public
#endif
 class ProgressBar : ControlBase
    {
        #region Fields
        #endregion

        #region Properties

        #region Value
        private int mValue = 75;
        /// <summary>
        /// Gets/Sets current value.
        /// </summary>
        [Browsable(true), DefaultValue(75), Category("Appearance"), Description("Current value")]
        public int Value
        {
            get { return mValue; }
            set
            {
                mValue = value;
                if (mValue < mMinValue) mValue = mMinValue;
                else if (mValue > mMaxValue) mValue = mMaxValue;
                #region Defensive tracing
                else
                {
                    //valid, move on
                }
                #endregion

                InvalidateLayout();

                OnValueChanged(value);
                RaiseValueChanged(value);
            }
        }
        #endregion

        #region Min Value
        private int mMinValue = 0;
        /// <summary>
        /// Gets/Sets minimum value.
        /// </summary>
        [Browsable(true), DefaultValue(0), Category("Appearance"), Description("Miminum value")]
        public int MinValue
        {
            get { return mMinValue; }
            set
            {
                mMinValue = value;
                if (mMinValue >= mMaxValue) mMinValue = mMaxValue - 1;
                #region Defensive tracing
                else
                {
                    //valid case
                }
                #endregion

                //ensure value is in valid range
                if (mValue < mMinValue) mValue = mMinValue;
                else if (mValue > mMaxValue) mValue = mMaxValue;
                #region Defensive tracing
                else
                {
                    //valid, move on
                }
                #endregion

                InvalidateLayout();
            }
        }
        #endregion

        #region Max Value
        private int mMaxValue = 100;
        /// <summary>
        /// Gets/Sets maximum value.
        /// </summary>
        [Browsable(true), DefaultValue(100), Category("Appearance"), Description("Maximum value")]
        public int MaxValue
        {
            get { return mMaxValue; }
            set
            {
                mMaxValue = value;

                if (mMaxValue <= mMinValue) mMaxValue = mMinValue + 1;
                #region Defensive tracing
                else
                {
                    //valid, move on
                }
                #endregion

                //ensure value is in valid range
                if (mValue < mMinValue) mValue = mMinValue;
                else if (mValue > mMaxValue) mValue = mMaxValue;
                #region Defensive tracing
                else
                {
                    //valid, move on
                }
                #endregion

                InvalidateLayout();
            }
        }
        #endregion

        #region Alignment
        private ContentAlignment mAlignment = ContentAlignment.MiddleCenter;
        /// <summary>
        /// Gets or Sets alignment of text
        /// </summary>
        [DefaultValue(ContentAlignment.MiddleCenter), Description("Gets or Sets alignment of text"), Category("Appearance")]
        public virtual ContentAlignment Alignment
        {
            get { return mAlignment; }
            set
            {
                if (mAlignment != value)
                {
                    mAlignment = value;
                    InvalidateLayout();
                }
            }
        }
        #endregion

        #region Text Style
        private TextStyle mTextStyle = TextStyle.Normal;
        /// <summary>
        /// Gets/Sets font style for rendering text.
        /// </summary>
        [Browsable(true), DefaultValue(TextStyle.Normal), Category("Appearance"), Description("Font style for rendering text")]
        public TextStyle TextStyle
        {
            get { return mTextStyle; }
            set
            {
                mTextStyle = value;
                Invalidate();
            }
        }
        #endregion

        #region Render Context
        private ProgressBarRenderContext mRenderContext;
        /// <summary>
        /// Gets reference to current renderation context.
        /// </summary>
        protected ProgressBarRenderContext RenderContext
        {
            get { return mRenderContext; }
        }
        #endregion

        #endregion

        #region Events

        #region Value Changed
        /// <summary>
        /// This event is raised after property Value is changed.
        /// </summary>
        public event ProgressBarValueChanged ValueChanged;

        /// <summary>
        /// Invoke event ValueChanged
        /// </summary>
        protected void RaiseValueChanged(int newValue)
        {
            ProgressBarValueChanged handler = ValueChanged;
            if (handler != null) handler(this, newValue);
        }
        #endregion

        #endregion

        #region On Value Changed
        /// <summary>
        /// This method is raised when property Value is changed.
        /// </summary>
        protected virtual void OnValueChanged(int newValue) { }
        #endregion

        #region Set Value
        /// <summary>
        /// Set property Value without raising event ValueChanged.
        /// </summary>
        /// <param name="value">The new value to be assigned</param>
        public void SetValue(int value)
        {
            mValue = value;
            if (mValue < mMinValue) mValue = mMinValue;
            else if (mValue > mMaxValue) mValue = mMaxValue;
            #region Defensive tracing
            else
            {
                //valid, move on
            }
            #endregion

            InvalidateLayout();

            OnValueChanged(value);
        }
        #endregion

        #region On Get Render Context
        /// <summary>
        /// Get reference to the renderation context which is used by <see cref="IControlRenderer"/>.
        /// </summary>
        protected override RenderContext OnGetRenderContext(Graphics canvas)
        {
            return mRenderContext;
        }
        #endregion

        #region On Calculate Layout
        /// <summary>
        /// Perform layout calculations neccessary for this control.
        /// </summary>
        protected override void OnCalculateLayout()
        {
            Rectangle rTrack = new Rectangle(0, 0, Width, Height);
            int width = (int)(((double)mValue / ((double)mMaxValue - (double)mMinValue)) * rTrack.Width);
            Rectangle rProgress = new Rectangle(rTrack.Left, rTrack.Top, width, rTrack.Height);

            //assign to render context
            mRenderContext.TrackRegion = rTrack;
            mRenderContext.ProgressRegion = rProgress;
        }
        #endregion

        #region Constructors
        /// <summary>
        /// Default constructor.
        /// </summary>
        public ProgressBar()
        {
            this.Size = new Size(150, 18);
            mRenderContext = new ProgressBarRenderContext();
        }
        #endregion
    }

    /// <summary>
    /// Handler for <see cref="ProgressBar"/> when property Value is changed.
    /// </summary>
    /// <param name="sender">The control that raised this event.</param>
    /// <param name="newValue">The newly assigned value.</param>
    public delegate void ProgressBarValueChanged(object sender, int newValue);
}
